package com.jmyd.sharding.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import com.jmyd.sharding.base.dto.KeywordQueryDto;
import com.jmyd.sharding.base.service.impl.BaseServiceImpl;
import com.jmyd.sharding.base.vo.PageBean;
import com.jmyd.sharding.domain.DomainPoFactory;
import com.jmyd.sharding.domain.dto.SysOrderDto;
import com.jmyd.sharding.domain.dto.SysOrderUpdateDto;
import com.jmyd.sharding.domain.po.SysOrder;
import com.jmyd.sharding.domain.vo.SysOrderVo;
import com.jmyd.sharding.mapper.SysOrderMapper;
import com.jmyd.sharding.service.ISysOrderService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author samphin
 * @date 2024/6/18 10:46
 */
@Service
public class SysOrderService extends BaseServiceImpl<String, SysOrderDto, SysOrder> implements ISysOrderService {

    @Resource
    private SysOrderMapper sysOrderMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(SysOrderDto dto) {
        SysOrder sysOrder = DomainPoFactory.newInstance(SysOrder.class).build(dto);
        sysOrder.setCode(RandomStringUtils.random(16, true, true));
        sysOrder.setDeleted(false);
        this.sysOrderMapper.insertSelective(sysOrder);
        return true;
    }

    @Override
    public boolean updateById(SysOrderUpdateDto dto) {
        SysOrder sysOrder = new SysOrder().build(dto);
        this.sysOrderMapper.updateByPrimaryKeySelective(sysOrder);
        return true;
    }

    @Override
    public PageBean<SysOrderVo> paging(KeywordQueryDto queryDto) {
        final Page<Object> page = PageMethod.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        final List<SysOrderVo> voList = this.sysOrderMapper.paging(queryDto);
        return new PageBean<>(voList, page);
    }

    @Override
    public SysOrderVo queryDetail(String id) {
        final SysOrder sysOrder = this.sysOrderMapper.selectById(id);
        return new SysOrderVo().buildVo(sysOrder);
    }

    @Override
    public boolean deleteById(String id) {
        this.sysOrderMapper.deleteById(id);
        return true;
    }
}